import java.util.ArrayList;
import java.util.List;

public class Solution89 {

    List<Integer> ans;
    int flag=0;

    public List<Integer> grayCode(int n) {
        int[] count=new int[1<<n];
        count[0]=1;
        List<Integer> list=new ArrayList<>();
        list.add(0);
        dfs(list,count,n);
        return ans;
    }

    void dfs(List<Integer> list,int[] count,int n){
        if (flag==1){
            return;
        }
        int t=list.get(list.size()-1);
        if (list.size()==1<<n){
            if ((1<<17)%t==0){
                ans=list;
                flag=1;
            }
            return;
        }
        for (int i=0;i<n;i++){
            int p=t^(1<<i);
            if (count[p]==0){
                count[p]=1;
                list.add(p);
                dfs(list,count,n);
                if (flag==1){
                    return;
                }
                list.remove(list.size()-1);
                count[p]=0;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution89().grayCode(2));
    }
}
